package com.cfm880.market.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.cfm880.market.R;
import com.cfm880.market.bean.App;
import com.daimajia.numberprogressbar.NumberProgressBar;
import com.facebook.drawee.view.SimpleDraweeView;
import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadSampleListener;
import com.liulishuo.filedownloader.FileDownloader;
import com.orhanobut.logger.Logger;

import java.io.File;
import java.util.List;

/**
 * Created by cfm on 16-10-11.
 */

public class AppRecyclerViewAdapter extends RecyclerView.Adapter<AppRecyclerViewAdapter.ViewHolder>{
    List<App> mList;
    Context mContext;
    public AppRecyclerViewAdapter(List<App> list) {
        this.mList = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        this.mContext = parent.getContext();
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.app_item, parent, false);

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.tv_appName.setText(mList.get(position).getApp_name());
        holder.tv_appIntro.setText(mList.get(position).getApp_intro());
        holder.mDraweeView.setImageURI(mList.get(position).getApp_icon());
        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.mProgressBar.setVisibility(View.VISIBLE);
                createDownloadTask(mList.get(position).getApp_url(), holder.mProgressBar).start();

            }
        });
        if (position==0){
            holder.root.requestFocus();
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        public View root;
        public SimpleDraweeView mDraweeView;
        public TextView tv_appName;
        public TextView tv_appIntro;
        public NumberProgressBar mProgressBar;
        public ViewHolder(View itemView) {
            super(itemView);
            root = itemView;
            mDraweeView = (SimpleDraweeView) itemView.findViewById(R.id.appIcon);
            tv_appName = (TextView) itemView.findViewById(R.id.tv_appName);
            tv_appIntro = (TextView) itemView.findViewById(R.id.tv_appIntro);
            mProgressBar = (NumberProgressBar) itemView.findViewById(R.id.number_progress_bar);
            root.setFocusable(true);
            root.setSelected(true);
            root.setClickable(true);

        }
    }

    private BaseDownloadTask createDownloadTask(String url, final NumberProgressBar numberProgressBar) {
        if (url == null){
            Toast.makeText(mContext, "地址为空", Toast.LENGTH_SHORT).show();
            return null;
        }
        final String filePath =Environment.getExternalStorageDirectory().getPath() +
                url.substring(url.lastIndexOf("/"), url.lastIndexOf(".apk")) + ".apk";
        return FileDownloader.getImpl().create(url)
                .setPath(filePath, false)
                .setCallbackProgressTimes(2*2000)
                .setMinIntervalUpdateSpeed(2*2000)
                .setListener(new FileDownloadSampleListener(){
                    @Override
                    protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                        super.pending(task, soFarBytes, totalBytes);
                    }

                    @Override
                    protected void completed(BaseDownloadTask task) {
                        super.completed(task);
                        numberProgressBar.setProgress(1);
                        numberProgressBar.setMax(1);
                        Toast.makeText(mContext, "开始安装《《《 @@。。",Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.fromFile(new File(filePath)),
                                "application/vnd.android.package-archive");
                        mContext.startActivity(intent);

                    }

                    @Override
                    protected void error(BaseDownloadTask task, Throwable e) {
                        super.error(task, e);
//                        Logger.d("error",e.getMessage());
                        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG).show();
                    }

                    @Override
                    protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                        super.progress(task, soFarBytes, totalBytes);
                        numberProgressBar.setMax(totalBytes);
                        numberProgressBar.setProgress(soFarBytes);
                    }
                    @Override
                    protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                        super.paused(task, soFarBytes, totalBytes);
                        numberProgressBar.setMax(totalBytes);
                        numberProgressBar.setProgress(soFarBytes);
                    }
                });
    }

    public void install(){

    }
}
